package com.sphd.Restaurant.utils;

import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.sphd.Restaurant.base.MyApplication;
import com.sphd.Restaurant.infos.IntegralInfo;
import com.sphd.Restaurant.infos.LoginInfo;
import com.sphd.Restaurant.infos.RestaurantInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hu on 2016/9/28.
 */

public class MyJson {

    SharedPreferences remb = MyApplication.getRemember();

    public LoginInfo getLoginInfo(JSONObject jsonObject) {
        LoginInfo login = new LoginInfo();
        try {
            JSONArray jay = jsonObject.getJSONArray("loginInfo");
            Log.e("JAY===", jay.length() + "");
            for (int i = 0; i < jay.length(); i++) {
                JSONObject jsob = jay.getJSONObject(i);
                if (jsob.has("user_phone"))
                    login.setUser_phone(getString("user_phone", jsob));
                if (jsob.has("user_sex"))
                    login.setUser_sex(getInt("user_sex", jsob));
                if (jsob.has("user_name")) {
                    if (TextUtils.isEmpty(jsob.getString("user_name"))) {
                        login.setUser_name("未填写");
                    } else {
                        login.setUser_name(getString("user_name", jsob));
                    }
                }

                if (jsob.has("user_photo"))
                    login.setUser_photo(getString("user_photo", jsob));
                if (jsob.has("id"))
                    login.setId(getInt("id",jsob));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return login;
    }
    /**
     * 我的资料json解析
     *
     * @param value
     * @return
     */
    public LoginInfo getMydata(String value) {
        LoginInfo info = null;
        try {
            JSONArray job = new JSONArray(value);
            info = new LoginInfo();
            for (int i = 0; i < job.length(); i++) {
                JSONObject object = job.getJSONObject(i);
//                Log.e("user_name", "user_name=" + object.getString("user_name"));
                info.setUser_name(object.getString("user_name"));
//                if (object.getString("user_sex").equals("1")) {
//                    info.setSex("男");
//                } else {
//                    info.setSex("女");
//                }
                info.setUser_sex(object.getInt("user_sex"));
                info.setUser_phone(object.getString("user_phone"));
                info.setUser_photo(object.getString("user_photo"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return info;
    }
    public List<IntegralInfo> getIndentSomething(JSONObject object) {
        List<IntegralInfo> infos = null;
        try {
            infos = new ArrayList<>();
            if (!object.getString("res").equals("[]")) {
                JSONArray jsonArray = new JSONArray(object.getString("res"));

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject job = jsonArray.getJSONObject(i);
                    IntegralInfo info = new IntegralInfo();
                    if (job.has("dishes")) {
                        info.setDishes_name(job.getString("dishes"));
                    }
                    if (job.has("regtime")) {

                        info.setRegtime(job.getLong("regtime"));
                    }
                    if (job.has("integral")) {
                        info.setMember_integral(job.getInt("integral"));
                    }
                    infos.add(info);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return infos;
    }

    public List<String> getCitiesList(JSONObject response) {
        List<String> list = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(response.getString("res"));
            for (int i = 0; i < array.length(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                list.add(jsonObject.getString("cg_city"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    private String getString(String key, JSONObject jsb) {
        try {
            if (jsb.has(key)) {
                return jsb.getString(key);
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return null;

    }


    private int getInt(String key, JSONObject jsb) {
        try {
            if (jsb.has(key)) {
                return jsb.getInt(key);
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return 0;

    }

    private boolean getBoolean(String key, JSONObject jsb) {
        try {
            if (jsb.has(key)) {
                return jsb.getBoolean(key);
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return false;

    }

    private double getDouble(String key, JSONObject jsb) {
        try {
            if (jsb.has(key)) {
                return jsb.getDouble(key);
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return 0;

    }

    private long getLong(String key, JSONObject jsb) {
        try {
            if (jsb.has(key)) {
                return jsb.getLong(key);
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return 0;

    }

    public List<RestaurantInfo> getAllRestaurantList(JSONObject job) {
        List<RestaurantInfo> list = null;
        try {
            list = JsonUtil.getListFromString(job.getString("res"), RestaurantInfo.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    public long getLong(String key) {
        if (remb.contains(jiami(key)))
            return remb.getLong(jiami(key), 0);
        return 0;
    }

    /**
     * @param val
     * @return 加密
     */
    private String jiami(String val) {
        return JiaMi.setJami(val);
    }

    /**
     * @param val
     * @return 解密
     */
    private String jiemi(String val) {
        return JiaMi.getJiemi(val);
    }
}
